<template>
	<view class="page">
		<fu-custom bgColor="bg-white" :isBack="false">
			<block slot="content">登录</block>
		</fu-custom>
		<view>
			<view class="header">
				<image src="/static/talent/login/logo.png"></image>
			</view>
			<view class="content">
				<view>申请获取以下权限</view>
				<text>获得你的手机号码信息</text>
			</view>
			<button class="bottom bg-green" type="primary" open-type="getPhoneNumber" withCredentials="true" lang="zh_CN"
			 @getphonenumber="getPhoneNumber" style="background:#39b54a;">
				绑定手机号
			</button>
		</view>
		<!-- <view class="text-center text-999 text-sm">
			<image @tap.stop="selectCheck" v-if="!check" class="check_img" src="/static/talent/home/payment_2.png" mode=""></image>
			<image @tap.stop="selectCheck" v-else class="check_img" src="/static/talent/home/payment.png" mode=""></image>
			<text>注册登录即同意</text>
			<text class="alink margin-lr-xs" @tap="navto(0)">《平台服务协议》</text>
			<text>和</text>
			<text class="alink margin-lr-xs" @tap="navto(1)">《隐私政策》</text>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '',
				option: '',
				batId: '',
				invite_code: '',
				// check: false,
			};
		},
		onLoad(option) {
			this.batId = option.batId
			this.invite_code = option.invite_code
			uni.login({
				success: res => {
					this.code = res.code;
				}
			});
		},
		methods: {
			// 取消登录
			cancle() {
				uni.navigateBack();
			},
			selectCheck() {
				this.check = !this.check;
			},
			// 获取手机号
			getPhoneNumber(e) {
				console.log(e);
				if (e.detail.errMsg.indexOf("user deny") !== -1) {
					uni.navigateBack();
				} else {
					this.bindMoblile(e.detail.encryptedData, e.detail.iv);
				}
			},
			bindMoblile(encryptedData, iv) {
				uni.showLoading({
					title: '绑定中...'
				})
				uni.getUserInfo({
					provider: 'weixin',
					success: infoRes => {
						console.log(infoRes)
						console.log(encryptedData)
						console.log(iv)
						console.log(this.code)
						console.log(JSON.stringify(uni.getStorageSync('Wxinfo_tmp')))
						if (this.code) {
							console.log(this.invite_code)
							if (this.invite_code == undefined || this.invite_code == '') {
								/* 用户信息发给后台 */
								this.$api.post(global.apiUrls.postMiniparamMobile, {
										encryptedData: encryptedData,
										iv: iv,
										code: this.code,
										wxinfo: JSON.stringify(uni.getStorageSync('Wxinfo_tmp')),
									})
									.then(res => {
										res = res.data;
										console.log('res', res);
										uni.hideLoading();
										if (res.code == 1) {
											this.$message.info(res.msg);
											uni.removeStorageSync('Wxinfo_tmp');
											global.userInfo = res.data.userinfo;
											uni.setStorageSync(
												'USER_INFO',
												res.data.userinfo
											);
											global.token = res.data.userinfo.user_token;
											// global.token = '9bd19fe6-678c-13fc-3736-56c3d5d05b50';
											uni.$emit('appLoginSuccess', () => {
											
											})
											// this.initializeSocket()
											uni.setStorageSync(
												'USER_TOKEN',
												res.data.userinfo.user_token
											);
											// console.log(123456789)
											// uni.navigateBack();
											setTimeout(function() {
												uni.switchTab({
													url: '/pages/tab/home/shopindex'
												});
											},500);
										} else {
											this.$message.info(res.msg);
										}
									})
									.catch(err => {
										uni.hideLoading();
										console.log('ERROR', err);
									})
							} else {
								this.$api.post(global.apiUrls.inviteCodePhone, {
										encryptedData: encryptedData,
										iv: iv,
										code: this.code,
										wxinfo: JSON.stringify(uni.getStorageSync('Wxinfo_tmp')),
										invite_code: this.invite_code
									})
									.then(res => {
										res = res.data;
										console.log('res', res);
										uni.hideLoading();
										if (res.code == 1) {
											this.$message.info(res.msg);
											uni.removeStorageSync('Wxinfo_tmp');
											global.userInfo = res.data.userinfo;
											uni.setStorageSync(
												'USER_INFO',
												res.data.userinfo
											);
											global.token = res.data.userinfo.user_token;
											// global.token = '9bd19fe6-678c-13fc-3736-56c3d5d05b50';
											uni.$emit('appLoginSuccess', () => {
											
											})
											// this.initializeSocket()
											uni.setStorageSync(
												'USER_TOKEN',
												res.data.userinfo.user_token
											);
											// uni.navigateBack();
											setTimeout(() => {
												uni.switchTab({
													url: '/pages/tab/home/shopindex'
												})
												// uni.redirectTo({
												// 	url: 'pages/index/batDetails?id=' + this.batId
												// })
											},500)
										} else {
											this.$message.info(res.msg);
										}
									})
									.catch(err => {
										uni.hideLoading();
										console.log('ERROR', err);
									})
							}
							
						} else {
							uni.hideLoading();
							this.$message.info('获取授权失败');
						}
					},
					fail: err => {
						uni.hideLoading();
						console.log('ERROR', err)
					}
				});
			},
			navto(id){
				this.$urouter.navigateTo({
				  url: '/pages/user/login/user-agreement',
				  params: {
					id: id
				  }
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.check_img {
		width: 30rpx;
		height: 30rpx;
		vertical-align: middle;
		margin-right: 16rpx;
		transform: translateY(-3rpx);
	}
	.page {
		min-height: 100vh;
		background: #fff;
	}

	.header {
		height: 172rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 220rpx 0 128rpx 0;

		image {
			width: 236rpx;
			height: 172rpx;
		}
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 114rpx;

		view:first-child {
			font-weight: bold;
		}
	}

	.content text {
		display: block;
		color: #666666;
		margin-top: 40rpx;
		font-size: 28rpx;
	}

	.alink {
		color: #009B77;
		font-weight: 800;
	}

	.bottom {
		border-radius: 8rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
		background: #006747 !important;
	}

	.text-999 {
		width: 100%;
		text-align: center;
		position: fixed;
		bottom: 80rpx;
		// margin-left: 20rpx;
		color: #6B6F73;
		font-size: 24rpx;
	}
</style>
